<template>
  <div class="lists">
    <el-table
      :data="userListData"
      style="width: 100%; margin-bottom: 20px"
      row-key="id"
      border
    >
      <el-table-column prop="id" label="编号" sortable width="180">
      </el-table-column>
      <el-table-column prop="username" label="名称" sortable> </el-table-column>
      <el-table-column prop="rolename" label="角色" sortable> </el-table-column>
      <el-table-column prop="status" label="状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status == 1">启用</el-tag>
          <el-tag v-if="scope.row.status == 2" type="danger">禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="primary" @click="editUser(scope.row.uid)">编辑</el-button>
          <el-button type="danger" @click="deleteUser(scope.row.uid)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :page-size="listQuery.size"
      :current-page="listQuery.page"
      @current-change="curChange"
      layout="prev, pager, next"
    :total="Number(userCount.total)"
    >
    </el-pagination>
  </div>
</template>

<script>
import { getUserList,getUserDelete } from "../../../request/api";
import {mapGetters,mapActions} from 'vuex'
export default {
  props:["dialog"],
  data() {
    return {
      
      // 查询参数
      listQuery: {
        size: 3, //查询条数
        page: 1, //页码数
      },
      // 管理员数据列表
      userListData: [],
    };
  },
  computed: {
    ...mapGetters({userCount:"User/userCount"})
  },
  methods: {
    // 获取管理员数据总数
    ...mapActions({getUserCountData:"User/getUserCountData"}),
   
    // 获取管理员数据列表
    getUserListData() {
      getUserList(this.listQuery).then(
        (res) => {
          if (res.data.code == 200) {
            this.userListData = res.data.list;
          }
        },
        (err) => err
      );
    },
    // 切换页码
    curChange(page) {
      this.listQuery.page = page;
      // 刷新列表
      this.getUserListData();
    },
    // 角色修改
    editUser(uid){
      this.$emit('editUser',uid)
    },
    // 删除管理员
    deleteUser(uid) {
      this.$confirm("是否删除该菜单?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      }).then(
        () => {
          getUserDelete({ uid }).then(
            (res) => {
              console.log(res);
              if (res.data.code == 200) {
                this.$message.success("删除成功");
                // 隐藏对话框
                this.dialog.isShowDialog = false;
                // 刷新数据列表
                this.listQuery.page=1
                this.getUserListData();
              }
            },
            (err) => err
          );
        },
        (err) => err
      );
    },
  },

  mounted() {
    this.getUserCountData();
    this.getUserListData();
  },
};
</script>

<style>

</style>